package com.ttdj.cmel.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ttdj.cmel.CMELApplication;
import com.ttdj.cmel.R;
import com.ttdj.cmel.bean.InformationBean;

import java.util.List;

public class InformationAdapter extends BaseAdapter {

    List<InformationBean> list;
    private LayoutInflater mInflater;
    private Context mContext;

    public InformationAdapter(List list, Context context) {
        this.list = list;
        Log.d("leihuiming", "list size = " + list.size());
        mContext= context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Log.d("leihuiming", "postion = " + position);
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.information_item_layout, parent, false); //加载布局
            holder = new ViewHolder();

            holder.titleTv = convertView.findViewById(R.id.title);
            holder.imageIV = convertView.findViewById(R.id.thum_img);

            convertView.setTag(holder);
        } else {   //else里面说明，convertView已经被复用了，说明convertView中已经设置过tag了，即holder
            holder = (ViewHolder) convertView.getTag();
        }
        InformationBean bean = list.get(position);
        holder.titleTv.setText(bean.getTitle());
        holder.imageIV.setImageResource(bean.getResetId());
        return convertView;
    }

    private class ViewHolder {
        TextView titleTv;
        ImageView imageIV;
    }
}
